package fr.coupedumonde2014.app.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import fr.coupedumonde2014.app.R;
import fr.coupedumonde2014.app.modele.Match;
import fr.coupedumonde2014.app.view.MatchView;
import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;


public class MatchAdapter extends BaseAdapter implements StickyListHeadersAdapter {

    public static int PARIS_1 = 0;
    public static int PARIS_NULL = 1;
    public static int PARIS_2 = 2;

    private List<Match> listMatches;
    private Context context;

    private Map<Integer,MatchView> cells = new HashMap<Integer,MatchView>();
    private MatchView.DismissCallBack dismissCallback;

    public MatchAdapter(List<Match> matches, Context context, MatchView.DismissCallBack dismissCallback) {
        this.context = context;
        this.listMatches = matches;
        this.dismissCallback = dismissCallback;
    }

    @Override
    public int getCount() {

        if (listMatches != null)
            return listMatches.size();

        return 0;
    }

    @Override
    public Object getItem(int i) {
        return listMatches.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {

        if(view == null) {
            LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.cell, parent, false);
        }

        if(cells.containsKey(position))
            cells.get(position).construire(view);

        else
            cells.put(position,new MatchView(context, view, (Match) getItem(position), position, this.dismissCallback));

        return view;
    }


    public void remove(int position) {
        listMatches.remove(position);
    }




    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {

        if(convertView == null)
            convertView = View.inflate(context,R.layout.header, null);

        boolean isFr = Locale.getDefault().getLanguage().equals("fr");

        if(isFr)
            ((TextView)convertView.findViewById(R.id.date)).setText(((Match) getItem(position)).getDate());
        else
            ((TextView)convertView.findViewById(R.id.date)).setText(((Match) getItem(position)).getDateEn());

        return convertView;
    }

    @Override
    public long getHeaderId(int position) {

        long id = ((Match) getItem(position)).getDateLong();

        Log.d("ID", id + "");

        //return the first character of the country as ID because this is what headers are based upon
        return id;
    }
}
